package com.wzy.service.imp;

import com.wzy.dao.KtQkDao;
import com.wzy.entity.KtQk;
import com.wzy.service.ExportService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
@Transactional
public class ExportServiceImpl implements ExportService {
    private KtQkDao ktQkDao;

    @Autowired
    public ExportServiceImpl(KtQkDao ktQkDao) {
        this.ktQkDao = ktQkDao;
    }

    @Override
    public void exportFile(HttpServletResponse response) {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        OutputStream os = null;
        XSSFWorkbook xWorkbook = null;
        try {
            String fileName = "ktqk" + df.format(new Date()) + ".xlsx";

            os = response.getOutputStream();
            //response.reset();

            response.setHeader("Content-disposition", "attachment; filename = " + URLEncoder.encode(fileName, "UTF-8"));
            //response.setContentType("application/octet-streem");

            xWorkbook = new XSSFWorkbook();
            XSSFSheet xSheet = xWorkbook.createSheet("ktqkList");

            // set Sheet页头部
            setSheetHeader(xWorkbook, xSheet);

            // set Sheet页内容
            setSheetContent(xWorkbook, xSheet);

            xWorkbook.write(os);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != os) {
                try {
                    os.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            if (null != xWorkbook) {
                try {
                    xWorkbook.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void setSheetHeader(XSSFWorkbook xWorkbook, XSSFSheet xSheet) {
        xSheet.setColumnWidth(0, 10 * 256);
        xSheet.setColumnWidth(1, 10 * 256);
        xSheet.setColumnWidth(2, 10 * 256);
        xSheet.setColumnWidth(3, 10 * 256);
        xSheet.setColumnWidth(4, 10 * 256);
        xSheet.setColumnWidth(5, 10 * 256);

        CellStyle cs = xWorkbook.createCellStyle();
//		// 设置水平垂直居中
//		cs.setAlignment(CellStyle.ALIGN_CENTER);
//		cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
//		// 设置字体
//		Font headerFont = xWorkbook.createFont();
//		headerFont.setFontHeightInPoints((short) 12);
//		headerFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
//		headerFont.setFontName("宋体");
//		cs.setFont(headerFont);
        cs.setWrapText(true);// 是否自动换行

        XSSFRow xRow0 = xSheet.createRow(0);

        XSSFCell xCell0 = xRow0.createCell(0);
        xCell0.setCellStyle(cs);
        xCell0.setCellValue("id");

        XSSFCell xCell1 = xRow0.createCell(1);
        xCell1.setCellStyle(cs);
        xCell1.setCellValue("kname");

        XSSFCell xCell2 = xRow0.createCell(2);
        xCell2.setCellStyle(cs);
        xCell2.setCellValue("yname");

        XSSFCell xCell3 = xRow0.createCell(3);
        xCell3.setCellStyle(cs);
        xCell3.setCellValue("state");

        XSSFCell xCell4 = xRow0.createCell(4);
        xCell4.setCellStyle(cs);
        xCell4.setCellValue("time");

        XSSFCell xCell5 = xRow0.createCell(5);
        xCell5.setCellStyle(cs);
        xCell5.setCellValue("rank");
    }

    @Override
    public void setSheetContent(XSSFWorkbook xWorkbook, XSSFSheet xSheet) {
        List<KtQk> ktQkList =ktQkDao.lists();
        CellStyle cs = xWorkbook.createCellStyle();
        cs.setWrapText(true);

        if (null != ktQkList && ktQkList.size() > 0) {
            for (int i = 0; i < ktQkList.size(); i++) {
                XSSFRow xRow = xSheet.createRow(i + 1);
                KtQk ktQk = ktQkList.get(i);
                for (int j = 0; j < 6; j++) {
                    XSSFCell xCell = xRow.createCell(j);
                    xCell.setCellStyle(cs);
                    switch (j) {
                        case 0:
                            xCell.setCellValue(ktQk.getId());
                            break;
                        case 1:
                            xCell.setCellValue(ktQk.getKname());
                            break;
                        case 2:
                            xCell.setCellValue(ktQk.getYname());
                            break;

                        case 3:
                            xCell.setCellValue(ktQk.getState());
                            break;
                        case 4:
                            xCell.setCellValue(ktQk.getTime());
                            break;
                        case 5:
                            xCell.setCellValue(ktQk.getRank());
                            break;

                        default:
                            break;
                    }
                }
            }
        }
    }

    @Override
    public String getCellValue(Cell cell) {
        String cellValue = "";
        // DecimalFormat df = new DecimalFormat("#");
        switch (cell.getCellType()) {
            case STRING:
                cellValue = cell.getRichStringCellValue().getString().trim();
                break;
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    Date tempValue = cell.getDateCellValue();
                    SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");
                    cellValue = simpleFormat.format(tempValue);
                } else {
                    cellValue = String.valueOf(cell.getNumericCellValue());
                }
                // cellValue = df.format(cell.getNumericCellValue()).toString();
                break;
            case BOOLEAN:
                cellValue = String.valueOf(cell.getBooleanCellValue()).trim();
                break;
            case FORMULA:
                cellValue = cell.getCellFormula();
                break;
            default:
                cellValue = "";
                break;
        }
        return cellValue;
    }
}
